//
//  PublicViewController.m
//  product_B
//
//  Created by lanou on 16/5/26.
//  Copyright © 2016年 lanou. All rights reserved.
//

#import "PublicViewController.h"
#import "RegistAndLoginViewController.h"
#import "MusicView.h"
#import "MusicPlayerModel.h"
#import <MediaPlayer/MediaPlayer.h>
#import "UserViewController.h"
#import "SearchWebViewController.h"
#import "SearchViewController.h"
#import "UserViewController.h"
@interface PublicViewController ()
@property (nonatomic, assign)BOOL isShow;
@property (nonatomic, strong)NSTimer *publicTime;
@property (nonatomic, assign)CGFloat angle;
@end

@implementation PublicViewController
- (instancetype)init{
    if (self = [super init]) {
        self.titleLabel = [[UILabel alloc]initWithFrame:CGRectMake((kWidth - 50) / 2, 25, 50, 30)];
        self.titleLabel.textAlignment = NSTextAlignmentCenter;
    }
    return self;
}
- (void)viewDidLoad {
    [super viewDidLoad];
    // Do any additional setup after loading the view.
    self.topView = [[UIView alloc]initWithFrame:CGRectMake(0, 0, kWidth, 64)];
    self.topView.backgroundColor = [UIColor grayColor];
    self.topView.alpha = 0.1;
    [self.view addSubview:self.topView];
    [self.view addSubview:_titleLabel];
    self.searchButton = [UIButton buttonWithType:UIButtonTypeSystem];
    self.searchButton.frame = CGRectMake(20, 30, 20, 20);
    [self.searchButton addTarget:self action:@selector(searchButtonAction:) forControlEvents:UIControlEventTouchUpInside];
    [self.searchButton setBackgroundImage:[UIImage imageNamed:@"搜索.png"] forState:UIControlStateNormal];
    [self.view addSubview:_searchButton];
    
    self.musicButton = [UIButton buttonWithType:UIButtonTypeSystem];
    self.musicButton.frame = CGRectMake(kWidth * 0.75, 30, 20, 20);
    [self.musicButton addTarget:self action:@selector(musicButtonAction:) forControlEvents:UIControlEventTouchUpInside];
    [self.musicButton setBackgroundImage:[UIImage imageNamed:@"音乐 旋转.png"] forState:UIControlStateNormal];
    self.musicButton.hidden = YES;
    [self.view addSubview:_musicButton];
    
    UIView *view = [[UIView alloc]initWithFrame:CGRectMake(0, 63, kWidth, 1)];
    view.backgroundColor = Kcolor(147, 147, 147);
    [self.view addSubview:view];
    
    self.userButton = [UIButton buttonWithType:UIButtonTypeSystem];
    self.userButton.frame = CGRectMake(kWidth - 40, 30, 20, 20);
    [self.userButton addTarget:self action:@selector(userButtonAction:) forControlEvents:UIControlEventTouchUpInside];
    [self.userButton setBackgroundImage:[UIImage imageNamed:@"用户.png"] forState:UIControlStateNormal];
    [self.view addSubview:_userButton];
    
    self.musicView = [[MusicView alloc]initWithFrame:CGRectMake(20, 74, kWidth - 40, 100)];
    
    self.musicView.backgroundColor = [UIColor whiteColor];

    self.musicView.layer.shadowOffset = CGSizeMake(1, 1);
    self.musicView.layer.shadowColor = [UIColor blackColor].CGColor;
    self.musicView.layer.shadowOpacity = 5;
    self.musicView.layer.shadowRadius = 1;
    self.musicView.layer.cornerRadius = 10;
    self.musicView.alpha = 0.8;
    [self.view addSubview:self.musicView];
    self.musicView.hidden = YES;
    UITapGestureRecognizer *tap = [[UITapGestureRecognizer alloc]initWithTarget:self action:@selector(tapAction:)];
    self.musicView.picture.userInteractionEnabled = YES;
    [self.musicView.picture addGestureRecognizer:tap];
    
    
    self.publicTime = [NSTimer scheduledTimerWithTimeInterval:0.1 target:self selector:@selector(publicTimeAction) userInfo:nil repeats:YES];
    
}

- (void)tapAction:(UITapGestureRecognizer *)tap{
    UserViewController *user = [[UserViewController alloc]init];
    user.modelID = [MusicPlayerManager shareInstance].author.user_id;
    [self.navigationController pushViewController:user animated:YES];
}

- (void)publicTimeAction{
    if ([MusicPlayerManager shareInstance].player.currentItem.duration.timescale == 0 || [MusicPlayerManager shareInstance].player.currentTime.timescale == 0) {
        return;
    }
    long long int currentTime = [MusicPlayerManager shareInstance].player.currentTime.value / [MusicPlayerManager shareInstance].player.currentTime.timescale;
    long long int allTime = [MusicPlayerManager shareInstance].player.currentItem.duration.value / [MusicPlayerManager shareInstance].player.currentItem.duration.timescale;
    
    if (currentTime == allTime) {
        [[MusicPlayerManager shareInstance]replaceItemWithUrlString:[MusicPlayerManager shareInstance].playUrl];
    }
    if ([MusicPlayerManager shareInstance].isPlay) {
        _angle = _angle + 0.05;
        CGAffineTransform transform = CGAffineTransformMakeRotation(_angle);
    self.musicView.picture.transform = transform;
    self.musicButton.transform = transform;
    }
}


- (void)searchButtonAction:(UIButton *)sender{
    SearchViewController *search = [[SearchViewController alloc]init];
    [self.navigationController pushViewController:search animated:YES];
}

- (void)viewWillAppear:(BOOL)animated{
    [super viewWillAppear:animated];
    if ([MusicPlayerManager shareInstance].isPlay) {
        self.musicButton.hidden = NO;
    }
    self.musicView.hidden = YES;
    _isShow = NO;
    self.navigationController.navigationBar.hidden = YES;
}


- (void)musicButtonAction:(UIButton *)sender{
//    _musicView
    [_musicView setMusicViewWithModel:[MusicPlayerManager shareInstance].musicModel];
    if (!_isShow) {
        self.musicView.hidden = NO;
        [self.view bringSubviewToFront:self.musicView];
        [self.musicView.button addTarget:self action:@selector(musicViewButtonAction:) forControlEvents:UIControlEventTouchUpInside];
        if ([MusicPlayerManager shareInstance].isPlay) {
            [self.musicView.button setBackgroundImage:[UIImage imageNamed:@"暂停.png"] forState:UIControlStateNormal];
        }else{
            [self.musicView.button setBackgroundImage:[UIImage imageNamed:@"播放.png"] forState:UIControlStateNormal];
        }
    }else{
        self.musicView.hidden = YES;
    }
    _isShow = !_isShow;
}

- (void)userButtonAction:(UIButton *)sender{
    
//    [UIView transitionFromView:self.tabBarController.view toView:login.view duration:1 options:UIViewAnimationOptionTransitionFlipFromLeft completion:nil];
    CATransition *transition = [CATransition animation];
    transition.duration = 0.7f;
    transition.timingFunction = [CAMediaTimingFunction functionWithName:kCAMediaTimingFunctionEaseInEaseOut];
    transition.type = @"oglFlip";
    transition.subtype = kCATransitionFromRight;
    transition.delegate = self;
    [self.tabBarController.view.layer addAnimation:transition forKey:nil];
    if ([[NSUserDefaults standardUserDefaults]objectForKey:@"name"] == nil) {
        RegistAndLoginViewController *login = [[RegistAndLoginViewController alloc]init];
    [self.navigationController pushViewController:login animated:YES];
    }else{
        UserViewController *user = [[UserViewController alloc]init];
        user.isShow = YES;
        [self.navigationController pushViewController:user animated:YES];
    }

}

- (void)musicViewButtonAction:(UIButton *)sender{
    if ([MusicPlayerManager shareInstance].isPlay) {
        [sender setBackgroundImage:[UIImage imageNamed:@"播放.png"] forState:UIControlStateNormal];
    }else{
        [sender setBackgroundImage:[UIImage imageNamed:@"暂停.png"] forState:UIControlStateNormal];
    }
    
    [[MusicPlayerManager shareInstance]playerPlayAndPause];
    
}

- (void)viewDidAppear:(BOOL)animated{
    [super viewDidAppear:animated];
    self.tabBarController.tabBar.hidden = NO;
}

- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

/*
#pragma mark - Navigation

// In a storyboard-based application, you will often want to do a little preparation before navigation
- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {
    // Get the new view controller using [segue destinationViewController].
    // Pass the selected object to the new view controller.
}
*/

@end
